//
//  SINARegisterViewController.m
//  sinashebao
//
//  Created by  androidlongs on 2017/12/12.
//  Copyright © 2017年  androidlongs. All rights reserved.
//

#import "SINARegisterViewController.h"
#import "SINAIdentitySelectViewController.h"
#import "SINARegisterInputCodeViewController.h"
#import "SINAServiceAgreementViewController.h"
#import "SINAOtherLoginPasswordViewController.h"

@interface SINARegisterViewController ()<UITextFieldDelegate>
//用户名
@property (weak, nonatomic) IBOutlet UITextField *userNameField;
@property (weak, nonatomic) IBOutlet UIView *inputPhoneView;
//下一步按钮
@property (weak, nonatomic) IBOutlet UIButton *registerButton;
//服务条款按钮
@property (weak, nonatomic) IBOutlet UIButton *serviceButton;

@property (weak, nonatomic) IBOutlet UILabel *serviceLabel;


@end

@implementation SINARegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (UI_IS_IPHONE6PLUS) {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_plus_bg"]  forBarMetrics:UIBarMetricsDefault];
    } else {
        [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bar_bg"]  forBarMetrics:UIBarMetricsDefault];
    }
    
    [self initHeaderView];
    
}
-(void) initHeaderView{
    self.navigationController.navigationBarHidden=NO;
    if (self.pageType==0) {
        self.navigationItem.title =@"注册";
        self.inputPhoneView.frame=CGRectMake(0,CGRectGetMaxY( self.serviceLabel.frame)+35 , mainW, 46);
        self.userNameField.frame=CGRectMake(15,0, mainW-30, 46);
        self.registerButton.frame=CGRectMake(15,CGRectGetMaxY( self.inputPhoneView.frame)+33 , mainW-30, 46);
    }else if(self.pageType==1){
        self.navigationItem.title =@"找回密码";
        self.inputPhoneView.frame=CGRectMake(0,CGRectGetMaxY( self.serviceLabel.frame)-22 , mainW, 46);
        self.userNameField.frame=CGRectMake(15,0, mainW-30, 46);
        self.registerButton.frame=CGRectMake(15,CGRectGetMaxY( self.inputPhoneView.frame)+33 , mainW-30, 46);
        self.serviceLabel.hidden=YES;
        self.serviceButton.hidden=YES;
        
    }
    
    //[super initBackItem];
    [self.view setBackgroundColor:[UIColor colorWithHexString:@"#eeeeee"]];
  
    NSMutableDictionary *userNameDic = [NSMutableDictionary dictionary];
    userNameDic[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#DDDDDD"];
    NSAttributedString *userNameAttri = [[NSAttributedString alloc] initWithString:@"请输入你的手机号" attributes:userNameDic];
    [self.userNameField setAttributedPlaceholder:userNameAttri];
    
    
    self.userNameField.delegate = self;
    
    //设置lable的圆角显示
    self.registerButton.layer.cornerRadius =5;
    //设置lable的边框 宽度为1
    self.registerButton.layer.borderWidth=0;
    //设置lable的边框颜色
    self.registerButton.layer.borderColor=[UIColor redColor].CGColor;
    //设置 显示大小范围为 border内容范围中
    self.registerButton.clipsToBounds = YES;
    [self.registerButton setBackgroundColor: baseButtonBackGroundColor];
    //self.navigationItem.leftBarButtonItem= backButton;
//    [self.serviceButton setTitleColor:baseButtonBackGroundColor forState:UIControlStateNormal];
    
    [[SINAUiviewClickLiserner alloc]viewClickFunction:self.serviceButton andWithBlick:^(UIView *view) {
        [self.navigationController pushViewController:[[SINAServiceAgreementViewController alloc]init] animated:YES];
    }];
    
}
-(void) backAction{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
   if (textField == self.userNameField) {
        if (self.userNameField.text.length >11) {
         self.userNameField.text=[self.userNameField.text substringWithRange:NSMakeRange(0, 11)];
        }
    }
    return YES;
    
}
//返回点击
- (IBAction)backClickFunction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
//下一步点击
- (IBAction)regisertClickFunction:(UIButton *)sender {
    
    [self.view endEditing:YES];
    NSString *userName = self.userNameField.text;
    
    if (userName==nil||[userName isEqualToString:@""]) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入手机号" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    if ([userName length]!=11) {
        WISAlertView *alertView =   [[WISAlertView alloc]initWithTitle:@"提示" message:@"请输入标准手机号" sureBtn:@"确定"
                                                             cancleBtn:nil];
        [alertView showXLAlertView];
        return;
    }
    
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:userName forKey:@"userPhone"];
    [userDefaults synchronize];
    
    if (self.otherLoginTag ==1 || self.otherLoginTag ==2) {  //第三方登录
        //检测手机号是否存在
        NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
        [dict setObject:userName forKey:@"phone"];
        [dict setObject:[NSNumber numberWithInteger:self.otherLoginTag] forKey:@"type"];
        
        WeakSelf;
        [CXAFNHttpRequestUtils httpGetRequest:[NSString stringWithFormat:@"%@%@",baseUrl,kPT_Interface_URI_isHasOpenid]  withParamters:dict success:^(id responseData) {
           
            NSString *code = responseData[@"code"];
            if ([code isEqualToString:@"1000"]) { //未注册
            
                //跳验证码页面
               
                SINARegisterInputCodeViewController *inputCodeController =[[SINARegisterInputCodeViewController alloc]initWithNibName:@"SINARegisterInputCodeViewController" bundle:nil];
                inputCodeController.phone = userName;
                inputCodeController.pageType=self.pageType;
                inputCodeController.otherLoginTag = self.otherLoginTag;
                [weakSelf.navigationController pushViewController:inputCodeController animated:YES];
            }
            else if([code isEqualToString:@"1022"]) //已注册未绑定
            {
                //输密码登录 -- 跳转输入密码页面
                SINAOtherLoginPasswordViewController * otherLoginPasswordViewController = [[SINAOtherLoginPasswordViewController alloc]init];
                otherLoginPasswordViewController.phoneNum = userName;
                otherLoginPasswordViewController.otherLoginTag = self.otherLoginTag;
                [weakSelf.navigationController pushViewController:otherLoginPasswordViewController animated:YES];
            }
            else if([code isEqualToString:@"1023"]) //已注册已绑定
            {
                NSString * message = nil;
                if (weakSelf.otherLoginTag ==1) {
                    message = @"此手机号已绑定其他QQ号";
                }
                else
                {
                    message = @"此手机号已绑定其他微信";
                }
                WISAlertView * alertView = [[WISAlertView alloc]initWithTitle:@"提示" message:message sureBtn:@"确定" cancleBtn:@"取消"];
                [alertView showXLAlertView];
                
            }
            
        } failure:^(NSError *error, NSInteger errCode) {
            
            
        }];
    }
    else
    {
        SINARegisterInputCodeViewController *inputCodeController =[[SINARegisterInputCodeViewController alloc]initWithNibName:@"SINARegisterInputCodeViewController" bundle:nil];
        inputCodeController.phone = userName;
        inputCodeController.pageType=self.pageType;
        [self.navigationController pushViewController:inputCodeController animated:YES];
    }
}
-(void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBarHidden = NO;
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
   
    NSString *userPhone= [userDefaults valueForKey:@"userPhone"];
    if (userPhone==nil||[userPhone isEqualToString:@"(null)"]) {
        userPhone=@"";
    }
    self.userNameField.text =userPhone;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
